package com.huatai.verity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.verity.entity.AuthObject;
import com.huatai.verity.mapper.AuthObjectMapper;
import com.huatai.verity.service.AuthObjectService;
import com.huatai.verity.vo.AuthObjectParmVo;
import org.springframework.stereotype.Service;

/**
 * @program: ht-system
 * @description: 授权对象接口
 * @author: songxiude
 * @create: 2023-07-21 15:57
 **/
@Service
public class AuthObjectServiceImpl extends ServiceImpl<AuthObjectMapper, AuthObject> implements AuthObjectService {

	/**
	 * 新增修改授权对象
	 * @param authObject
	 * @return
	 */
	public boolean saveUpdate(AuthObject authObject) {
		//如果id不传视为新增
		if (ObjectUtil.isEmpty(authObject.getId()) || authObject.getId().longValue()<=0) {
			authObject.setId(IdWorker.getId());
		}
		return saveOrUpdate(authObject);
	}

	/**
	 * 获取授权对象列表
	 * @param authObjectParmVo
	 * @return
	 */
	public IPage<AuthObject> getAuthObjects(AuthObjectParmVo authObjectParmVo) {

		long p=authObjectParmVo.getPage()>0?authObjectParmVo.getPage():1;
		long size= authObjectParmVo.getSize()>0?authObjectParmVo.getSize():10;
		Page<AuthObject> page = Page.of(p, size);
		LambdaQueryWrapper<AuthObject> queryWrapper = new LambdaQueryWrapper<>();
		//授权对象
		queryWrapper.like(StringUtil.isNotBlank(authObjectParmVo.getAuthObject()),AuthObject::getAuthObject,authObjectParmVo.getAuthObject());
		//单位
		queryWrapper.like(StringUtil.isNotBlank(authObjectParmVo.getUnitName()),AuthObject::getUnitName,authObjectParmVo.getUnitName());
		//手机号
		queryWrapper.like(StringUtil.isNotBlank(authObjectParmVo.getPhoneNumber()),AuthObject::getPhoneNumber,authObjectParmVo.getPhoneNumber());

		queryWrapper.orderByDesc(AuthObject::getCreateTime);
		return baseMapper.selectPage(page,queryWrapper);
	}
}
